<?php
    require_once dirname(__FILE__) . '/shared/config/config.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_affiliate.php';
    require_once DOCUMENT_ROOT . DIR_INCLUDE . 'check_user_login.php';
    require_once DOCUMENT_ROOT . DIR_INCLUDE . 'html_generator.php';
    $title = 'Affiliate System';

    $login = $_SESSION['user_info'];

    $btn_register_affiliate = filter_input(INPUT_POST, 'btn_register_affiliate');

    $success_info = "";
    
    if (!empty($btn_register_affiliate)) {
        $question_1 = filter_input(INPUT_POST, 'txt_question_1');
        $question_2 = filter_input(INPUT_POST, 'txt_question_2');
        $question_3 = filter_input(INPUT_POST, 'txt_question_3');
        $question_4 = filter_input(INPUT_POST, 'txt_question_4');
        $question_5 = filter_input(INPUT_POST, 'txt_question_5');
        $error = api_affiliate::validate_affiliate_form_fields($question_1, $question_2, $question_3,
                $question_4, $question_5);
        if (empty($error)) {
            if (api_affiliate::register_affiliate($login[0], $question_1, $question_2,
                    $question_3, $question_4, $question_5)) {
                $success_info = "Your Register is submitted to Admin. Please wait for Admin's approvement.";
            }
        }
    }
?>

<!DOCTYPE html>
<!-- IE SECTION -->

<!--[if lt IE 7]>
<html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>
<html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>
<html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js"> <!--<![endif]-->

                     <!-- END IE SECTION -->
<html>
<head>
    <?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'head.php' ?>
</head>
<body>
<?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'header.php' ?>

<!-- Title -->
<div class="section section-black">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="calltoaction-wrapper">
                    <h3>Affiliate</h3>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- End Title -->

<!-- Match Detail -->
<div class="section">
    <div class="container">
        <?php
        
            if (!api_affiliate::is_affiliate($login[0])) {
                show_message($success_info, $error, 3, 6);
                ?>
        <div class="row">
            <div class="col-md-offset-3 col-sm-6">
                <?php
                show_register_affiliate_form(FALSE, 0);
                ?>
            </div>
        </div>
                <?php
            } else {
                $client_list = api_affiliate::get_list_client($login[0]);
                $aff_priviledge = api_affiliate::get_aff_priviledge($login[0]);
                show_affiliate_info($login, $client_list, $aff_priviledge);
            }
        ?>
    </div>
</div>
<!-- End Match Detail -->

<?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php' ?>
</body>
</html>